﻿<UserControl x:Class="VSTalk.Engine.View.MainControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:VSTalk.Engine.View"
             xmlns:conerters="clr-namespace:VSTalk.Engine.Converters"
             xmlns:settings ="clr-namespace:VSTalk.Engine.Properties"
             xmlns:viewModel="clr-namespace:VSTalk.Engine.ViewModel"
             xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             >
  <UserControl.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="..\Resources\Icons.xaml"/>
      </ResourceDictionary.MergedDictionaries>

      <settings:Settings x:Key="Settings"/>

      <conerters:CommonStateToActionNameConverter x:Key="CommonStateToActionNameConverter"/>
      <conerters:VisibleIfFalseConverter x:Key="VisibleIfFalseConverter"/>
      <conerters:VisibleIfTrueConverter x:Key="VisibleIfTrueConverter"/>

      <DataTemplate x:Key="ConnectionBar">
        <Grid>
          <Button Content="{Binding CommonState, Converter={StaticResource CommonStateToActionNameConverter}}"
              Command="{Binding ChangeConnectionState}" Focusable="False"/>
        </Grid>
      </DataTemplate>

      <DataTemplate DataType="{x:Type viewModel:MainViewModel}">
        <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="30"/>
          </Grid.RowDefinitions>
          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto"/>
              <ColumnDefinition Width="*"/>
              <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Button Style="{DynamicResource MetroCircleButtonStyle}"
                  Height="30"
                    Grid.Column="0"
                  Width="30"
                    ToolTip="Settings"
                    Command="{Binding ShowAccountsWindow}">
              <Rectangle Width="10"
                    Height="10">
                <Rectangle.Fill>
                  <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_connect}" />
                </Rectangle.Fill>
              </Rectangle>
            </Button>

            <TextBox Grid.Column="1" controls:TextboxHelper.Watermark="Search" Margin="5" Text="{Binding SearchQuery, UpdateSourceTrigger=PropertyChanged}"
                     Style="{DynamicResource QueryMetroTextBox}"/>

            <CheckBox IsChecked="{Binding Source={StaticResource Settings}, Path=Default.ShowOfflineContacts}"
                      Content="Show offline" Command="{Binding ChangeAppSettings}"
                      Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Right" FontSize="10"
                      Margin="0,0,5,0"/>
          </Grid>

          <Grid Grid.Row="1" Visibility="{Binding HasConnections, Converter={StaticResource VisibleIfFalseConverter}}">
            <StackPanel Orientation="Vertical">
              <TextBlock HorizontalAlignment="Center">
                <Hyperlink Command="{Binding AddNewAccount}">Connect to ...</Hyperlink>
              </TextBlock>
            </StackPanel>
          </Grid>

          <Grid Grid.Row="1" Visibility="{Binding HasConnections, Converter={StaticResource VisibleIfTrueConverter}}">
            <local:ContactList DataContext="{Binding ContactList}"/>
          </Grid>

          <Grid Grid.Row="2" Visibility="{Binding HasConnections, Converter={StaticResource VisibleIfTrueConverter}}">
            <local:NotificationControl x:Name="notificationControl" DataContext="{Binding NotificationView}"/>
          </Grid>

          <ContentControl Grid.Row="3" ContentTemplate="{StaticResource ConnectionBar}" Content="{Binding}"
                          Visibility="{Binding HasConnections, Converter={StaticResource VisibleIfTrueConverter}}"/>
        </Grid>
        <DataTemplate.Triggers>
          <DataTrigger Binding="{Binding NotificationView.HasNotifications}" Value="True">
            <DataTrigger.EnterActions>
              <BeginStoryboard>
                <Storyboard>
                  <DoubleAnimation Storyboard.TargetName="notificationControl"
                               Storyboard.TargetProperty="Height"
                               From="0" To="100" Duration="0:0:1">
                    <DoubleAnimation.EasingFunction>
                      <SineEase EasingMode="EaseIn"/>
                    </DoubleAnimation.EasingFunction>
                  </DoubleAnimation>
                </Storyboard>
              </BeginStoryboard>
            </DataTrigger.EnterActions>
            <DataTrigger.ExitActions>
              <BeginStoryboard>
                <Storyboard>
                  <DoubleAnimation Storyboard.TargetName="notificationControl"
                               Storyboard.TargetProperty="Height"
                               From="100" To="0" Duration="0:0:1">
                    <DoubleAnimation.EasingFunction>
                      <SineEase EasingMode="EaseOut"/>
                    </DoubleAnimation.EasingFunction>
                  </DoubleAnimation>
                </Storyboard>
              </BeginStoryboard>
            </DataTrigger.ExitActions>
          </DataTrigger>
        </DataTemplate.Triggers>
      </DataTemplate>
    </ResourceDictionary>
  </UserControl.Resources>
  <ContentPresenter Content="{Binding}"/>
</UserControl>
